package com.wbao.dianniu.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import com.wbao.dianniu.R;
import com.wbao.dianniu.data.QuestAnswerListData;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.AddorDelCollectManager;

/**
 * 评论长点击处理类
 * <p>
 * Created by yejianjun on 2017/10/10.
 */

public class CommentLongClickHelper {


    private static CommentLongClickHelper instance;
    public ICommentLongListener mListener = null;
    public ITwoCommentListener twoCommentListenr = null;

    public static CommentLongClickHelper getInstance() {
        if (null == instance) {
            instance = new CommentLongClickHelper();
        }
        return instance;
    }

    public interface ICommentLongListener {
        public void commentLongClick(QuestAnswerListData data);
    }

    public void addCommentLongClickListener(ICommentLongListener listener) {
        mListener = listener;
    }

    public void removeCommentLongClickListener(ICommentLongListener listener) {
        mListener = null;
    }

    public interface ITwoCommentListener {
        public void twoCommentDel(QuestAnswerListData data);
    }

    public void addTwoCommentListener(ITwoCommentListener listener) {
        twoCommentListenr = listener;
    }

    public void removeTwoCommentListener() {
        twoCommentListenr = null;
    }

    /**
     * @param context
     * @param data        数据
     * @param reportType  举报类型
     * @param replyName   @用户名
     * @param commentType 1: 一级回复 2： 二级回复
     */
    public void commentLongClick(final Context context, final QuestAnswerListData data, final int reportType, final String replyName, final int commentType, final int answerListType) {
        final Dialog dialog = new Dialog(context, R.style.custom_Dialog);
        try {
            Window win = dialog.getWindow();
            win.getDecorView().setPadding(0, 0, 0, 0);
            win.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
            WindowManager.LayoutParams lp = win.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            win.setAttributes(lp);
            win.setWindowAnimations(R.style.buttom_animation);
        } catch (Exception e) {
            e.printStackTrace();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.custom_bottom_dialog, null);
        Button replyBtn = (Button) view.findViewById(R.id.custom_bottom_reply);
        View replyLine = (View) view.findViewById(R.id.custom_bottom_reply_line);
        Button copyBtn = (Button) view.findViewById(R.id.custom_bottom_copy);
        View copyLine = (View) view.findViewById(R.id.custom_bottom_copy_line);
        Button reportBtn = (Button) view.findViewById(R.id.custom_bottom_report);
        View reportLine = (View) view.findViewById(R.id.custom_bottom_report_line);
        Button deleteBtn = (Button) view.findViewById(R.id.custom_bottom_delete);
        final Button cancelBtn = (Button) view.findViewById(R.id.custom_bottom_cancel);

        // 自己的评论 显示删除和复制
        if (data.userInfo.accountId == GlobalContext.getAccountId()) {
            replyBtn.setVisibility(View.VISIBLE);
            replyLine.setVisibility(View.VISIBLE);
            copyBtn.setVisibility(View.VISIBLE);
            copyLine.setVisibility(View.VISIBLE);
            reportBtn.setVisibility(View.GONE);
            reportLine.setVisibility(View.GONE);
            deleteBtn.setVisibility(View.VISIBLE);
        } else { // 别人的评论，显示 回复，复制，举报 和取消
            replyBtn.setVisibility(View.VISIBLE);
            replyLine.setVisibility(View.VISIBLE);
            copyBtn.setVisibility(View.VISIBLE);
            copyLine.setVisibility(View.VISIBLE);
            reportBtn.setVisibility(View.VISIBLE);
            reportLine.setVisibility(View.VISIBLE);
            deleteBtn.setVisibility(View.GONE);
        }

        /*Window win = dialog.getWindow();
        win.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.x = 0;
        lp.y = 0;
        lp.width = (int) context.getResources().getDisplayMetrics().widthPixels; // 宽度
        view.measure(0, 0);
        lp.height = view.getMeasuredHeight();
        lp.alpha = 9f; // 透明度
        win.setAttributes(lp);

        dialog.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        dialog.getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int visibility) {
                int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                        //布局位于状态栏下方
                        View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
                        //全屏
                        View.SYSTEM_UI_FLAG_FULLSCREEN |
                        //隐藏导航栏
                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
                if (Build.VERSION.SDK_INT >= 19) {
                    uiOptions |= 0x00001000;
                } else {
                    uiOptions |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
                }
                dialog.getWindow().getDecorView().setSystemUiVisibility(uiOptions);
            }
        });*/

        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);

        // 回复
        replyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
                if (1 == commentType) { // 一级回复--点击回复--跳转到二级回复列表
                    int id = 0;
                    if (answerListType == AnswerListType.TYPE_KNOW) {
                        id = data.knowledgeId;
                        // 跳转到干货回复列表
//                        Intent intent = new Intent();
//                        intent.setClass(context, KnowAnswerActivity.class);
//                        context.startActivity(intent);
                    } else if (answerListType == AnswerListType.TYPE_QUEST || answerListType == AnswerListType.TYPE_AM) {
                        id = data.questId;
                    }
                    // 跳转到问答回复列表
                    ActivityUtils.intoTwoCommentActivity(context, id, data.id, answerListType, data);
                } else if (2 == commentType) { // 二级回复
                    ReplyHelper.getInstance().reply(replyName, data.accountId, data.id);
                }
            }
        });
        // copy
        copyBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
                ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
                cmb.setText(data.content);
                Notification.showToastMsg(context, "内容已复制");
            }
        });
        // 举报
        reportBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
                ActivityUtils.intoReportActivity(context, reportType, data.id);
            }
        });
        // 取消
        cancelBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });
        deleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
                // 一级回复删除通知
                if (1 == commentType) {
                    if (null != mListener) {
                        mListener.commentLongClick(data);
                    }
                } else if (2 == commentType) { // 二级回复删除
                    if (null != twoCommentListenr) {
                        twoCommentListenr.twoCommentDel(data);
                    }
                }


            }
        });
        dialog.setContentView(view);
        dialog.show();

    }

    /**
     * 显示复制和取消
     */
    public Dialog showCopyDialog(final Context context, final String content) {
        final Dialog dialog = new Dialog(context, R.style.custom_Dialog);
        try {
            Window win = dialog.getWindow();
            win.getDecorView().setPadding(0, 0, 0, 0);
            win.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
            WindowManager.LayoutParams lp = win.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            win.setAttributes(lp);
            win.setWindowAnimations(R.style.buttom_animation);
        } catch (Exception e) {
            e.printStackTrace();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.custom_bottom_dialog, null);
        Button replyBtn = (Button) view.findViewById(R.id.custom_bottom_reply);
        View replyLine = (View) view.findViewById(R.id.custom_bottom_reply_line);
        Button copyBtn = (Button) view.findViewById(R.id.custom_bottom_copy);
        View copyLine = (View) view.findViewById(R.id.custom_bottom_copy_line);
        Button reportBtn = (Button) view.findViewById(R.id.custom_bottom_report);
        View reportLine = (View) view.findViewById(R.id.custom_bottom_report_line);
        Button deleteBtn = (Button) view.findViewById(R.id.custom_bottom_delete);
        Button cancelBtn = (Button) view.findViewById(R.id.custom_bottom_cancel);

        replyBtn.setVisibility(View.GONE);
        replyLine.setVisibility(View.GONE);
        copyBtn.setVisibility(View.VISIBLE);
        copyLine.setVisibility(View.VISIBLE);
        reportBtn.setVisibility(View.GONE);
        reportLine.setVisibility(View.GONE);
        deleteBtn.setVisibility(View.GONE);

        /*Window win = dialog.getWindow();
        win.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.x = 0;
        lp.y = 0;
        lp.width = (int) context.getResources().getDisplayMetrics().widthPixels; // 宽度
        view.measure(0, 0);
        lp.height = view.getMeasuredHeight();
        lp.alpha = 9f; // 透明度
        win.setAttributes(lp);

        dialog.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        dialog.getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int visibility) {
                int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                        //布局位于状态栏下方
                        View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
                        //全屏
                        View.SYSTEM_UI_FLAG_FULLSCREEN |
                        //隐藏导航栏
                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
                if (Build.VERSION.SDK_INT >= 19) {
                    uiOptions |= 0x00001000;
                } else {
                    uiOptions |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
                }
                dialog.getWindow().getDecorView().setSystemUiVisibility(uiOptions);
            }
        });*/

        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        // 复制
        copyBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
                ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
                cmb.setText(content);
                Notification.showToastMsg(context, "内容已复制");
            }
        });
        // 取消
        cancelBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });
        dialog.setContentView(view);
        dialog.show();

        return dialog;
    }

    /**
     * 显示举报弹出框
     *
     * @param context
     * @param type
     * @param id
     */
    public void showReportDialog(final Context context, final int type, final int id) {
        final Dialog dialog = new Dialog(context, R.style.custom_Dialog);
        try {
            Window win = dialog.getWindow();
            win.getDecorView().setPadding(0, 0, 0, 0);
            win.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
            WindowManager.LayoutParams lp = win.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            win.setAttributes(lp);
            win.setWindowAnimations(R.style.buttom_animation);
        } catch (Exception e) {
            e.printStackTrace();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.custom_bottom_dialog, null);
        Button replyBtn = (Button) view.findViewById(R.id.custom_bottom_reply);
        View replyLine = (View) view.findViewById(R.id.custom_bottom_reply_line);
        Button copyBtn = (Button) view.findViewById(R.id.custom_bottom_copy);
        View copyLine = (View) view.findViewById(R.id.custom_bottom_copy_line);
        Button reportBtn = (Button) view.findViewById(R.id.custom_bottom_report);
        View reportLine = (View) view.findViewById(R.id.custom_bottom_report_line);
        Button deleteBtn = (Button) view.findViewById(R.id.custom_bottom_delete);
        Button cancelBtn = (Button) view.findViewById(R.id.custom_bottom_cancel);

        replyBtn.setVisibility(View.GONE);
        replyLine.setVisibility(View.GONE);
        copyBtn.setVisibility(View.GONE);
        copyLine.setVisibility(View.GONE);
        reportBtn.setVisibility(View.VISIBLE);
        reportLine.setVisibility(View.VISIBLE);
        deleteBtn.setVisibility(View.GONE);

        /*Window win = dialog.getWindow();
        win.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.x = 0;
        lp.y = 0;
        lp.width = (int) context.getResources().getDisplayMetrics().widthPixels; // 宽度
        view.measure(0, 0);
        lp.height = view.getMeasuredHeight();
        lp.alpha = 9f; // 透明度
        win.setAttributes(lp);

        dialog.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        dialog.getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int visibility) {
                int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                        //布局位于状态栏下方
                        View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
                        //全屏
                        View.SYSTEM_UI_FLAG_FULLSCREEN |
                        //隐藏导航栏
                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
                if (Build.VERSION.SDK_INT >= 19) {
                    uiOptions |= 0x00001000;
                } else {
                    uiOptions |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
                }
                dialog.getWindow().getDecorView().setSystemUiVisibility(uiOptions);
            }
        });*/

        dialog.setCanceledOnTouchOutside(true);
        reportBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
                ActivityUtils.intoReportActivity(context, type, id);
            }
        });
        // 取消
        cancelBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });
        dialog.setContentView(view);
        dialog.setCancelable(true);
        dialog.show();

    }


    /**
     * 显示收藏 举报弹出框
     */
    public void showCollectReportDialog(final Context context, final AddorDelCollectManager collectManager, int isCollect, final int questId) {
        final Dialog dialog = new Dialog(context, R.style.custom_Dialog);
        try {
            Window win = dialog.getWindow();
            win.getDecorView().setPadding(0, 0, 0, 0);
            win.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
            WindowManager.LayoutParams lp = win.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            win.setAttributes(lp);
            win.setWindowAnimations(R.style.buttom_animation);
        } catch (Exception e) {
            e.printStackTrace();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.custom_bottom_dialog, null);
        Button replyBtn = (Button) view.findViewById(R.id.custom_bottom_reply);
        View replyLine = (View) view.findViewById(R.id.custom_bottom_reply_line);
        Button copyBtn = (Button) view.findViewById(R.id.custom_bottom_copy);
        View copyLine = (View) view.findViewById(R.id.custom_bottom_copy_line);
        Button reportBtn = (Button) view.findViewById(R.id.custom_bottom_report);
        View reportLine = (View) view.findViewById(R.id.custom_bottom_report_line);
        Button deleteBtn = (Button) view.findViewById(R.id.custom_bottom_delete);
        Button cancelBtn = (Button) view.findViewById(R.id.custom_bottom_cancel);

        replyBtn.setVisibility(View.GONE);
        replyLine.setVisibility(View.GONE);
        copyBtn.setVisibility(View.VISIBLE);
        copyLine.setVisibility(View.VISIBLE);
        reportBtn.setVisibility(View.VISIBLE);
        reportLine.setVisibility(View.VISIBLE);
        deleteBtn.setVisibility(View.GONE);

        if (1 == isCollect) {
            copyBtn.setText("取消收藏");
        } else {
            copyBtn.setText("收藏");
        }

        /*Window win = dialog.getWindow();
        win.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.x = 0;
        lp.y = 0;
        lp.width = (int) context.getResources().getDisplayMetrics().widthPixels; // 宽度
        view.measure(0, 0);
        lp.height = view.getMeasuredHeight();
        lp.alpha = 9f; // 透明度
        win.setAttributes(lp);

        dialog.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        dialog.getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int visibility) {
                int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                        //布局位于状态栏下方
                        View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
                        //全屏
                        View.SYSTEM_UI_FLAG_FULLSCREEN |
                        //隐藏导航栏
                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
                if (Build.VERSION.SDK_INT >= 19) {
                    uiOptions |= 0x00001000;
                } else {
                    uiOptions |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
                }
                dialog.getWindow().getDecorView().setSystemUiVisibility(uiOptions);
            }
        });*/

        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        // 收藏
        copyBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
                collectManager.addorDelCollect(GlobalContext.getAccountId(), questId);
            }
        });
        reportBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
                ActivityUtils.intoReportActivity(context, ReportType.TYPE_QA, questId);
            }
        });
        // 取消
        cancelBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });
        dialog.setContentView(view);
        dialog.show();

    }


}
